package com.shanzmoo.base.bean;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.service.contexts.SecurityContext;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger 公共配置
 * @author: Gzhao 2020/12/21
 */
public class SwaggerConfigBase {

    private static final String version = "0.1.0";

    /**
     * 全局参数
     */
    public static List<ApiKey> security() {
        List<ApiKey> apiKeys = new ArrayList<>();
        apiKeys.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeys;
    }

    /**
     * 摘要信息
     */
    public static ApiInfo apiInfo(String title) {
        return new ApiInfoBuilder()
                .title(title)
                .version(version)
                .build();
    }

    public static List<SecurityContext> securityContexts() {
        ArrayList<SecurityContext> securityContexts = new ArrayList<>();
        SecurityContext sc = SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!auth).*$"))
                .build();
        securityContexts.add(sc);
        return securityContexts;
    }

    private static List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }
}
